// miniprogram/pages/test/test.js
import Approval from "../../core/Approval.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projects: getApp().globalData.projects,
    selectProject: getApp().globalData.nowPrj,
    current: 'check',
    parameter: [{ id: 0, name: '进行中', state: "wait" }, { id: 1, name: '待核销', state: "nextDone"}, 
      { id: 2, name: '已完成', state: "success" }, { id: 3, name: '已取消', state: "cancel"}],
    selectParameter:0,

//shenpi
    approvalList:null,
    approvalListCopy:null,
    loading:true,
    currentTab: 0, //预设当前项的值

//


    current_scroll:1,
    open: false,
        // mark 是指原点x轴坐标
    markX: 0,
        // newmark 是指移动的最新点的x轴坐标 
    newmarkX: 0,
    markY: 0,
    newmarkY: 0,
    istoright: true,

    winHeight: "",//窗口高度
 
    scrollLeft: 0, //tab标题的滚动条位置

    //抽屉
    hiddenDataPicker:true,
    selectApproval:null,
    TunnelDrawerIndex:0,
    swiperIndex: 0,
    indicatorDots: true,  //小点
    autoplay: true,  //是否自动轮播
    interval: 300000,  //间隔时间
    duration: 300,  //滑动时间
    circular: true,
  },
  //刷新
  onRefresh() {
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.init();
  },
  init(){
    this.data.parameter[0].checked = true;
    this.setData({
      parameter: this.data.parameter,
      selectParameter: 0
    })//默认parameter数组的第一个对象是选中的
    //---------------------------------------
    this.setData({
      projects: getApp().globalData.projects,
      selectProject: getApp().globalData.nowPrj,
    })

    console.log(this.data.selectProject);
    var that = this;
    // 高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    this.initData();
  },
  onLoad: function () {
   this.init();
  }, 
  TunnelDrawerClick(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      TunnelDrawerIndex: index
    })
  },
  viewA(e){

    console.log(e);
    let item = new Approval(e.currentTarget.dataset.vitem) ;
    console.log(item);
    item.getImgs()
    .then(
      res=>{
        this.setData({
          selectApproval: item,
          hiddenDataPicker: false
        })

      }
    )

  },
  previewImg(e) {
    let index = e.target.dataset.index;
    wx.previewImage({
      current: e.target.dataset.list[index],
      urls: e.target.dataset.list,
    })
  },
  // 参数点击响应事件
  parameterTap: function (e) {//e是获取e.currentTarget.dataset.id所以是必备的，跟前端的data-id获取的方式差不多
    var that = this
    var this_checked = e.currentTarget.dataset.id
    var parameterList = this.data.parameter//获取Json数组
    for (var i = 0; i < parameterList.length; i++) {
      if (parameterList[i].id == this_checked) {
        parameterList[i].checked = true;//当前点击的位置为true即选中
      }
      else {
        parameterList[i].checked = false;//其他的位置为false
      }
    }
    that.setData({
      parameter: parameterList,
      selectParameter:this_checked
    })
    this.updateData();
  },
  initData(){
    Approval.loadList_my()
    .then(
      res=>{
        console.log(res);
        res.forEach(x=>{
          let find = getApp().globalData.sys.ruletemplate_list.find(y => y.GuidID == x.ApprovalRule)
          if (find){
            x.ApprovalRuleName = find.Remark;
        }
        })
        this.setData({
          approvalList:res
        })
        this.updateData();
      }
    )
  },
  updateData(){
    // this.setData({
    //   loading: true,
    // })
    let approvalListCopy = [];
    let value=null;
    switch (this.data.currentTab){
      case 0: value="合同审批"; break;
      case 1: value = "支付审批";break;
      case 2: value = "事项审批";break;
      default:break;
    }
    for (let i = 0; i < this.data.approvalList.length;i++){
      if ((this.data.approvalList[i].nowState.state == this.data.parameter[this.data.selectParameter].state)&&(this.data.approvalList[i].Approval_Type_Guid==value)){
        approvalListCopy.push(this.data.approvalList[i]);
      }
    }
    wx.nextTick(()=>{
      this.setData({
        loading:false,
        approvalListCopy: approvalListCopy
      })
      console.log(this.data.approvalListCopy)
      //隐藏loading 提示框
      wx.hideLoading();
      //隐藏导航条加载动画
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    })
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
    this.updateData();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
      this.updateData();
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
  }
})